<template>
  <div class="login-container">
    <div class="login-header">
      <div class="logo-text">
        <img src="../../assets/images/personal_top_logo.png" />
        <span>长沙长泰</span>
      </div>
    </div>
    <div class="login-form-wrapper">
      <div class="login-form-box">
        <div class="input-item">
          <i class="iconfont iconxiugaishoujihaoma-01"></i>
          <input type="text" placeholder="请输入手机号码" maxlength="11" v-model="account" />
        </div>
        <div class="input-item">
          <i class="iconfont iconxiugaimima-01"></i>
          <input type="password" placeholder="请输入密码" maxlength="16" v-model="password" />
        </div>
        <div class="remember-password">
          <van-checkbox v-model="rememberPwd" shape="square">记住密码</van-checkbox>
        </div>
        <van-button class="login-btn" block @click="loginFn">登 录</van-button>
      </div>
    </div>
  </div>
</template>

<script>
import md5 from 'md5';
import { Checkbox, Button } from 'vant';
import { login } from '@/api/login';

export default {
  name: 'Login',
  components: {
    [Checkbox.name]: Checkbox,
    [Button.name]: Button
  },
  data() {
    return {
      account: '',
      password: '',
      rememberPwd: false
    };
  },
  created() {
    let rememberPassword = localStorage.getItem('rememberPassword');
    if (rememberPassword === '1') {
      let loginUserInfo = JSON.parse(localStorage.getItem('loginUserInfo'));
      this.account = loginUserInfo.account;
      this.password = loginUserInfo.password;
      this.rememberPwd = rememberPassword;
    }
  },
  methods: {
    // 登录
    loginFn() {
      if (!this.account) {
        this.$toast('请输入手机号码');
        return;
      }
      let phoneReg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
      if (!phoneReg.test(this.account)) {
        this.$toast('请输入正确的手机号码');
        return;
      }
      if (!this.password) {
        this.$toast('请输入密码');
        return;
      }
      login({
        account: this.account,
        autoLogin: true,
        password: md5(this.password)
      }).then(async res => {
        this.$toast.loading({
          message: '登录中...',
          duration: 0,
          forbidClick: true,
          loadingType: 'spinner'
        });
        localStorage.setItem('token', res.data.token);
        localStorage.setItem('customerIdForApp', res.data.customerId);
        localStorage.setItem('userInfo', JSON.stringify(res.data));
        // 获取用户权限
        await this.$store.dispatch('generateAhthButtons');

        // 判断是否有应用权限
        if (this.$store.state.apps.length) {
          let appsRequestName = this.$store.state.apps[0].request_name;
          let tabbarArr = [
            {
              requestName: 'iot_project',
              path: '/projectCenter'
            },
            {
              requestName: 'iot_maintenance',
              path: '/maintenanceCenter'
            },
            {
              requestName: 'iot_device',
              path: '/deviceCenter'
            }
          ];
          for (let i = 0; i < tabbarArr.length; i++) {
            let item = tabbarArr[i];
            if (item.requestName === appsRequestName) {
              this.$router.push(item.path);
              this.$store.commit('resetTabbarActive', 0);
            }
          }
        } else {
          this.$router.push('/my');
          this.$store.commit('resetTabbarActive', 0);
        }

        // 记住密码
        if (this.rememberPwd) {
          let loginUserInfo = {
            account: this.account,
            password: this.password
          };
          localStorage.setItem('rememberPassword', '1');
          localStorage.setItem('loginUserInfo', JSON.stringify(loginUserInfo));
        } else {
          localStorage.removeItem('rememberPassword');
          localStorage.removeItem('loginUserInfo');
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.login-container {
  width: 100%;
  height: 100%;
  background-color: #fff;

  .login-header {
    width: 100%;
    height: 3.626667rem;
    padding: 0 0.533333rem;
    background: linear-gradient(269.83deg, #0076e3 2.41%, #0076e3 29.51%, #3569f0 95.31%);

    .logo-text {
      display: flex;
      align-items: center;
      padding-top: 0.426667rem;

      img {
        width: 2.826667rem;
        height: 1.066667rem;
      }

      span {
        margin-left: 0.213333rem;
        color: #fff;
        font-size: 0.48rem;
      }
    }
  }

  .login-form-wrapper {
    width: 100%;
    margin-top: -1.76rem;
    padding: 0 0.533333rem;

    .login-form-box {
      width: 100%;
      background-color: #fff;
      padding: 0.64rem 0.426667rem;
      border-top-left-radius: 0.266667rem;
      border-top-right-radius: 0.266667rem;

      .input-item {
        display: flex;
        align-items: center;
        width: 100%;
        height: 1.2rem;
        padding: 0 0.533333rem;
        border-bottom: 1px solid var(--border-color);

        i {
          flex-shrink: 0;
          margin-right: 0.266667rem;
          color: #cccccc;
          font-size: 0.48rem;
        }

        input {
          flex: 1;
          font-size: 0.426667rem;
          line-height: normal;
          border: none;
          outline: none;
        }

        input::-webkit-input-placeholder {
          color: #ccc;
        }

        input::-moz-input-placeholder {
          color: #ccc;
        }

        input::-ms-input-placeholder {
          color: #ccc;
        }
      }

      .remember-password {
        margin: 0.4rem 0 0.533333rem;
        color: #333333;
        font-size: 0.373333rem;

        // ::v-deep .van-checkbox {
        //   .van-checkbox__icon {
        //     height: 0.373333rem;

        //     .van-icon {
        //       width: 0.373333rem;
        //       height: 0.373333rem;
        //     }
        //   }
        // }
      }

      .login-btn {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100%;
        height: 1.066667rem;
        color: #fff;
        font-size: 0.426667rem;
        background-color: var(--color-primary);
        border-radius: 0.106667rem;
      }
    }
  }
}
</style>
